package com.csun.cmny.provider.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 作者 :
 * @createDate 创建时间：
 * 类说明 -- 百度地图经纬度和地址相互转换的工具类
 */
public class BaiduMapUtils {
    /**
     * 百度地图通过地址来获取经纬度，传入参数address
     * @param address
     * @return
     */
    public static Map<String,Double> getLngAndLat(String address){
        Map<String,Double> map=new HashMap<String, Double>();
        String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=ULxDz5szeXwgAyqVVQCcyIMonaIrbRsk";
        String json = loadJSON(url);
        JSONObject obj = JSONObject.fromObject(json);
        System.out.println(obj.toString());
        if(obj.get("status").toString().equals("0")){
            double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
            double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
            map.put("lng", lng);
            map.put("lat", lat);
            System.out.println("经度：" + lng + "--- 纬度：" + lat);
        }else{
            System.out.println("未找到相匹配的经纬度！");
        }
        return map;
    }
    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (Exception e) {
        }
        return json.toString();
    }

    public static int timeByDistance (double originLongitude,double originLatitude,double destinationLongitude,double destinationLatitude) {
        String url = "http://api.map.baidu.com/directionlite/v1/riding?origin="+originLatitude+","+originLongitude+"&destination="+destinationLatitude+","+destinationLongitude+"&ak=jq3ClhIbtsO0sMXd7MCYY68ZAn30rOWq";
        System.out.println("路径"+url);
        String json = loadJSON(url);
        JSONObject obj = JSONObject.fromObject(json);
        System.out.println(obj.toString());
        int time=0;
        if(obj.get("status").toString().equals("0")){
            //time = obj.getJSONObject("result").getJSONObject("routes").getString("duration");
            JSONArray routes=obj.getJSONObject("result").getJSONArray("routes");
            time = routes.getJSONObject(0).getInt("duration");
        }else{
            System.out.println("未能成功计算所需时间!");
        }
       return time;
    }

    public static void main(String[] args) {


        Integer s = timeByDistance(116.339303, 40.01116, 116.452562, 39.936404);
        System.out.println(s);
    }

}
